package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.InvoiceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: InvoiceServiceImpl
 * @Description: 发票管理
 * @Author
 * @Date 2020/11/23
 * @Version 1.0
 */
@Service(timeout = 200000)
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceDao invoiceDao;
    @Autowired
    private PackingDao packingDao;
    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ShippingDao shippingDao;

    @Override
    public PageInfo<Invoice> findByPage(InvoiceExample invoiceExample, Integer pageNum,
                                        Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Invoice> invoices = invoiceDao.selectByExample(invoiceExample);
        return new PageInfo<>(invoices);
    }

    @Override
    public Invoice findById(String invoiceId) {
        return invoiceDao.selectByPrimaryKey(invoiceId);
    }

    @Override
    public Map<String, Object> delete(String id) {
        Map map = new HashMap();
        Invoice invoice = invoiceDao.findById(id);
        if (invoice.getStatus() != 0) {
            map.put("message", 0);
        } else {
            Packing packing = packingDao.selectByPrimaryKey(id);
            ExportExample exportExample = new ExportExample();
            exportExample.createCriteria().andIdIn(Arrays.asList(packing.getExportIds().split(",")));
            List<Export> exportList = exportDao.selectByExample(exportExample);
            if (exportList != null && exportList.size() > 0) {
                for (Export export : exportList) {
                    export.setState(5);
                    exportDao.updateByPrimaryKeySelective(export);
                }
            }
            ShippingExample shippingExample = new ShippingExample();
            Shipping shipping = shippingDao.selectByPrimaryKey(id);
            shipping.setState(0);
            shippingDao.updateByPrimaryKey(shipping);
            invoiceDao.deleteByPrimaryKey(id);
            map.put("message", 1);
        }
        return map;
    }

    @Override
    public void save(Invoice invoice) {
        //判断状态
        //invoice.setInvoiceId(UUID.randomUUID().toString());
        Shipping shipping = shippingDao.selectByPrimaryKey(invoice.getInvoiceId());
        shipping.setState(1);
        shippingDao.updateByPrimaryKeySelective(shipping);
        Packing packing = packingDao.selectByPrimaryKey(invoice.getInvoiceId());
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(Arrays.asList(packing.getExportIds().split(",")));
        List<Export> exportList = exportDao.selectByExample(exportExample);
        if (exportList != null && exportList.size() > 0) {
            for (Export export : exportList) {
                export.setState(6);
                exportDao.updateByPrimaryKeySelective(export);
            }
        }
        invoice.setStatus(0);
        invoiceDao.insertSelective(invoice);
    }

}
